package com.taptech.doufu.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.sdk.universalimageloader.core.ImageLoader;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.NovelSectionListBean;
import com.taptech.doufu.db.history.Read_HirstoryUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by op0509 on 2016/6/29.
 */
public class SectionNovelListAdapter extends BaseListAdapter {
    private Context mContext;
    private String novelId="";
    public List<NovelSectionListBean> mainList;
    private LayoutInflater mLayoutInflater;
    protected ImageLoader imageLoader = null;
    private NovelSectionListBean bean;
    public HashMap<String, String > hirstoryList;

    private int alreadTitleColorNight = WeMediaApplication.getInstance().getResources().getColor(R.color.text_dark);
    private int normalTitleColorNight = WeMediaApplication.getInstance().getResources().getColor(R.color.title_text_dark);

    private int alreadTitleColor = Color.parseColor("#bebebe");
    private int normalTitleColor = Color.parseColor("#666666");

    public SectionNovelListAdapter(Context mContext, String articleId) {
        this.mContext = mContext;
        novelId = articleId;
        Read_HirstoryUtil mRead_HirstoryUtil=new Read_HirstoryUtil(mContext);
        try {
            hirstoryList=mRead_HirstoryUtil.queryHirstory(novelId);
        }catch (Exception e){

        }

        mLayoutInflater = LayoutInflater.from(mContext);
        mainList = new ArrayList<>();

    }

    public void setHirstory(String sectionid){
        if (hirstoryList!=null) {
            hirstoryList.put(sectionid, sectionid);
        }
    }
    @Override
    public int getCount() {
        return mainList != null ? mainList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return 0;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        NovelHolder novelHolder;
        if (convertView == null) {
            convertView=mLayoutInflater.inflate(R.layout.item_section_novel,null);
            novelHolder=new NovelHolder();
            novelHolder.novelTitle=(TextView)convertView.findViewById(R.id.item_section_novel_title);
            novelHolder.novelWordCount = (TextView) convertView.findViewById(R.id.item_section_novel_word_count);
            novelHolder.tvTime = (TextView) convertView.findViewById(R.id.item_section_novel_time);
            novelHolder.imgPay = (ImageView) convertView.findViewById(R.id.flag_2);
            convertView.setTag(novelHolder);
        } else {
            novelHolder=(NovelHolder)convertView.getTag();
        }
        bean=mainList.get(position);
        String id=bean.getId();
        novelHolder.novelWordCount.setText(bean.getLength() + "字");
        novelHolder.novelTitle.setText(bean.getTitle());//+"("+(position+1)+")");
        if(bean.getVip() == 1){
            if(bean.isNeed_pay()){
                novelHolder.imgPay.setImageResource(R.drawable.img_chapter_locked_xhdpi);
            }else{
                novelHolder.imgPay.setImageResource(R.drawable.img_chapter_locked_op);
            }
            novelHolder.imgPay.setVisibility(View.VISIBLE);
        } else {
            novelHolder.imgPay.setVisibility(View.INVISIBLE);
        }
        novelHolder.tvTime.setText(bean.getAdd_time());
        int titleColor = normalTitleColor;
        if (hirstoryList!=null&&hirstoryList.containsKey(id)) {
            if(WeMediaApplication.getInstance().isDayNightMode_Night){
                titleColor = alreadTitleColorNight;
            }else {
                titleColor = alreadTitleColor;
            }

        }else{
            if(WeMediaApplication.getInstance().isDayNightMode_Night){
                titleColor = normalTitleColorNight;
            }else {
                titleColor = normalTitleColor;
            }
        }
        novelHolder.novelTitle.setTextColor(titleColor);
        return convertView;
    }
    public void stopActivity(){
        ((Activity)mContext).finish();
    }
    public synchronized void setListDate(List<NovelSectionListBean> mainList) {
        this.mainList.clear();
        this.mainList.addAll(mainList);
        notifyDataSetInvalidated();
    }


    public class NovelHolder extends ContentViewHolder {
        TextView novelTitle;
        TextView novelWordCount;
    }

    public void addHeaderView(View header) {
        if (header == null) {
            return;
        }

        this.mHeaderView = header;
        this.mHasHeaderView = true;

        notifyDataSetChanged();
    }

    public class ContentViewHolder {
        public NovelSectionListBean homeBean;
        public TextView commentCount;
        public TextView tvTime;
        public ImageView imgPay;
    }

    public NovelSectionListBean getBean(int postion) {
        NovelSectionListBean topBean = null;
        if (postion >= 0 && mainList != null && postion < mainList.size()) {
            topBean = this.mainList.get(postion);
        }
        return topBean;
    }

    public String getNovelId() {
        return novelId;
    }
}
